<?php
/**
 * 上传图片
 *
 * @author xue
 */
namespace Ttc\Controller;
use Think\Controller;
class UploadController extends Controller {
    public function index(){
        $this->meta_title = '上传图片';
        $this->display('index');
    }
    /**
     * 上传图片
     */
    public function UploadImages(){
    	// var_dump($_FILES);exit;

    	/* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

        /* 调用文件上传组件上传文件 */
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');

        $info = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器
        // var_dump($info);

        $path = C('PICTURE_UPLOAD');
        $img_path = 'http://'.$_SERVER['HTTP_HOST'].$info['file']['path'];
        // var_dump($img_path);exit;

        /* 记录图片信息 */
        if($info){
            $return['status'] = 1;
            $return['img_id'] = $info['file']['id'];
        	$return['path'] = $img_path; 
            // $return = array_merge($info['download'], $return);
        } else {
            $return['status'] = 0;
            $return['info']   = $Picture->getError();
        }

    	// var_dump($return);

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
        // var_dump($this->ajaxReturn($return));

    }

    /**
     * base64上传图片
     */
    public function base64UploadImages(){
        // header('Content-type:text/html;charset=utf-8');
        $base64_image_content = $_POST['base64Img'];
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
            $type = $result[2];
            // $new_file = C('PICTURE_UPLOAD')['rootPath'].date('Y-m-d',time())."/";
            $new_file = C('PICTURE_UPLOAD')['rootPath'].date('Y-m-d',time())."/";
            
            if(!file_exists($new_file))
            {
                //检查是否有该文件夹，如果没有就创建，并给予最高权限
                mkdir($new_file, 0700);
            }
            $new_file = $new_file.time().".{$type}";
            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
                //存数据库 路径:$new_file
                $data['path'] = $new_file;
                $data['create_time'] = time();
                $res = D('Picture')->add($data);
                if(!$res){
                    $return['status'] = 0;
                    $return['info']   = '上传失败';
                }else{
                    $img_path = 'http://'.$_SERVER['HTTP_HOST'].$data['path'];
                    $return['status'] = 1;
                    $return['img_id'] = $res;
                    $return['path'] = $img_path; 
                }

                $this->ajaxReturn($return);
            }else{
                $return['status'] = 0;
                $return['info']   = '上传失败';
                $this->ajaxReturn($return);
            }
        }

    }
}